package com.uchengwang.ui.cm_ui.uu.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.uu.adapter.SystemNotifyAdapter;
import com.uchengwang.ui.cm_ui.uu.bean.SystemNotifyBean;
import com.uchengwang.ui.cm_ui.uu.http.UUHttpManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/8/10.
 * 系统通知界面
 */

public class SystemNotifyActivity extends BaseWithTitleActivity{
    private PtrClassicFrameLayout layout;
    private RecyclerAdapterWithHF mAdapter;
    private RecyclerView invite_rv;
    private SystemNotifyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_group_invite);
        setTitle("通知");

        initView();
    }

    private void initView(){
        layout = (PtrClassicFrameLayout) findViewById(R.id.invite_layout);
        invite_rv = (RecyclerView) findViewById(R.id.invite_rv);

        initData();
    }

    private void initData(){
        adapter = new SystemNotifyAdapter(this);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        invite_rv.setLayoutManager(new LinearLayoutManager(this));
        invite_rv.setAdapter(mAdapter);


        layout.postDelayed(new Runnable() {

            @Override
            public void run() {
                layout.autoRefresh(true);
            }
        }, 150);

        layout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                layout.refreshComplete();
                checkData();
            }
        });

    }

    /**
     *查询数据库 拿到最后一条数据
     */
    private void checkData(){
        long time = System.currentTimeMillis();
        List<SystemNotifyBean> list = new ArrayList<>();
        list.add(new SystemNotifyBean(R.mipmap.uchenglogo, "Uni团队", "暂无通知信息", time, false));
//        list.add(new SystemNotifyBean(R.drawable.notify_join, "参与号", "暂无评论信息", time, false));
//        list.add(new SystemNotifyBean(R.drawable.notify_group, "群助手", "暂无群信息", time, false));
//        if (!UCommonUtil.getUtil().isUni()){
//            list.add(new SystemNotifyBean(R.drawable.notify_club, "社团号", "暂无社团信息", time, false));
//        }
        adapter.setData(list);
//        QueryBuilder<SystemNotifyBean> builder = new QueryBuilder<>(SystemNotifyBean.class);
//        builder.appendOrderDescBy("_id");
        getSystem();
    }

    /**
     * 获取新的系统通知
     * */
    public void getSystem(){
        UUHttpManager.getManager().getSystemNotifys(this, new UUHttpManager.SystemListener() {
            @Override
            public void onResult(List<SystemNotifyBean> list) {
                if (list != null && list.size() >0){
                    adapter.setData(list);
                }
            }
        });
    }

}
